﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLTTTH.Entity;
using QLTTTH.Config;
using System.Data;
using System.Data.SqlClient;

namespace QLTTTH.DAO
{
    class PhatTrenDAO:DAOBase
    {
        public DataSet GetAll()
        {
            return Select("select * from PhatTren");
        }

        public PhatTren GetById(PhatTren phattren)
        {
            PhatTren ptren = null;
            SqlConnection conn = null;
            try
            {
                conn = Config.Config.getConnection();
                conn.Open();
                SqlCommand commnad = new SqlCommand("select * from PhatTren where MaKenh = '" + phattren.MaKenh + "' and MaCT = '" + phattren.MaCT + "'", conn);
                SqlDataReader dataReader = commnad.ExecuteReader();
                if (dataReader.Read())
                {
                    ptren = new PhatTren();
                    ptren.MaCT = dataReader.GetString(0);
                    ptren.MaKenh = dataReader.GetString(1);
                    ptren.ThoiGianPS = dataReader.GetDateTime(2);
                    ptren.ThoiLuongPS = dataReader.GetInt32(3);
                    return ptren;
                }
            }
            catch (Exception ex)
            {

            }
            finally
            {
                conn.Close();

            }
            return ptren;
        }

        public bool Insert(PhatTren ptren)
        {

            String sql = "INSERT INTO [QLVT].[dbo].[PhatTren]([MaKenh],[MaTanSo][ThoiGianPS])"
               + "VALUES('" + ptren.MaCT + "','" + ptren.MaKenh + "','" + ptren.ThoiGianPS + "'," + ptren.ThoiLuongPS + ")";
            return Update(sql);
        }

        public bool DeleteById(PhatTren ptren)
        {
            return Update("delete from PhatTren where MaKenh = '" + ptren.MaKenh + "' and MaCT = '" + ptren.MaCT  + "'");
        }

        public bool UpdateById(PhatTren ptren)
        {
            String sql = "update PhatTren "
                 + " set ThoiGianPS = '" + ptren.ThoiGianPS + "'"
                 + " set ThoiLuongPS = '" + ptren.ThoiLuongPS + "'"
                 + " where MaKenh = '" + ptren.MaKenh + "' and MaTanSo = '" + ptren.MaCT + "'";
            return Update(sql);
        }
    }
}
